﻿module HtmlUnescaper

open System
open System.Text.RegularExpressions
open HtmlEntites

let namedEntitiesMap = 
    let lines = spec.Split([|"\r\n"|], StringSplitOptions.RemoveEmptyEntries)
    let map = System.Collections.Generic.Dictionary<string, string>()
    for line in lines do
        let items = line.Split([|';'|], StringSplitOptions.RemoveEmptyEntries) |> Array.toList
        let name = items.[0]
        let hex = items.[1]
        let num = Convert.ToInt32(hex, 16)
        map.[name] <- Char.ConvertFromUtf32(num)
    map

let namedEntityEvaluator (m:Match) = 
    let name = m.Groups.[1].Value
    if namedEntitiesMap.ContainsKey(name)
    then namedEntitiesMap.[name]
    else "&amp;#x" + name + ";"

let decimalEntityEvaluator (m:Match) = 
    let num = Int32.Parse(m.Groups.[1].Value)
    Char.ConvertFromUtf32(num)

let hexEntityEvaluator (m:Match) = 
    let hex = m.Groups.[1].Value
    let num = Convert.ToInt32(hex, 16)  
    Char.ConvertFromUtf32(num)


let unescape html = 
    let html = Regex.Replace(html, "&#(\d+);", decimalEntityEvaluator)
    let html = Regex.Replace(html, "&#x([a-fA-F0-9]+);", hexEntityEvaluator)
    let html = Regex.Replace(html, "&(\w+);", namedEntityEvaluator)
    html

